package com.dianping.cat.home.service.transform;

import com.dianping.cat.home.service.IEntity;
import com.dianping.cat.home.service.IVisitor;
import com.dianping.cat.home.service.entity.Domain;
import com.dianping.cat.home.service.entity.ServiceReport;
import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;















public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

/*  35 */   public DefaultXmlBuilder() { this(false); }



/*  39 */   public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }

  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  43 */     this.m_compact = compact;
/*  44 */     this.m_sb = sb;
/*  45 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }

  public String buildXml(IEntity<?> entity) {
/*  49 */     entity.accept(this.m_visitor);
/*  50 */     return this.m_sb.toString();
  }

  protected void endTag(String name) {
/*  54 */     this.m_level--;

/*  56 */     indent();
/*  57 */     this.m_sb.append("</").append(name).append(">\r\n");
  }


/*  61 */   protected String escape(Object value) { return escape(value, false); }


  protected String escape(Object value, boolean text) {
/*  65 */     if (value == null) {
/*  66 */       return null;
    }

/*  69 */     String str = toString(value);
/*  70 */     int len = str.length();
/*  71 */     StringBuilder sb = new StringBuilder(len + 16);

/*  73 */     for (int i = 0; i < len; i++) {
/*  74 */       char ch = str.charAt(i);

/*  76 */       switch (ch) {
        case '<':
          sb.append("&lt;");
          break;
        case '>':
/*  81 */           sb.append("&gt;");
          break;
        case '&':
/*  84 */           sb.append("&amp;");
          break;
        case '"':
/*  87 */           if (!text) {
/*  88 */             sb.append("&quot;");
            break;
          }
        default:
/*  92 */           sb.append(ch);
          break;
      }

    }
/*  97 */     return sb.toString();
  }

  protected void indent() {
/* 101 */     if (!this.m_compact) {
/* 102 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 103 */         this.m_sb.append("   ");
      }
    }
  }


/* 109 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 113 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 117 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }


  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 121 */     indent();

/* 123 */     this.m_sb.append('<').append(name);

/* 125 */     int len = nameValues.length;

/* 127 */     for (int i = 0; i + 1 < len; i += 2) {
/* 128 */       Object attrName = nameValues[i];
/* 129 */       Object attrValue = nameValues[i + 1];

/* 131 */       if (attrValue != null) {
/* 132 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 136 */     if (dynamicAttributes != null) {
/* 137 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 138 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }

/* 142 */     if (text != null && closed) {
/* 143 */       this.m_sb.append('>');
/* 144 */       this.m_sb.append(escape(text, true));
/* 145 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 147 */       if (closed) {
/* 148 */         this.m_sb.append('/');
      } else {
/* 150 */         this.m_level++;
      }

/* 153 */       this.m_sb.append(">\r\n");
    }
  }


  protected String toString(Object value) {
/* 159 */     if (value instanceof String)
/* 160 */       return (String)value; 
/* 161 */     if (value instanceof Collection) {
/* 162 */       Collection<Object> list = (Collection)value;
/* 163 */       StringBuilder sb = new StringBuilder(32);
/* 164 */       boolean first = true;

/* 166 */       for (Object item : list) {
/* 167 */         if (first) {
/* 168 */           first = false;
        } else {
/* 170 */           sb.append(',');
        }

/* 173 */         if (item != null) {
/* 174 */           sb.append(item);
        }
      }

/* 178 */       return sb.toString();
/* 179 */     }  if (value.getClass().isArray()) {
/* 180 */       int len = Array.getLength(value);
/* 181 */       StringBuilder sb = new StringBuilder(32);
/* 182 */       boolean first = true;

/* 184 */       for (int i = 0; i < len; i++) {
/* 185 */         Object item = Array.get(value, i);

/* 187 */         if (first) {
/* 188 */           first = false;
        } else {
/* 190 */           sb.append(',');
        }

/* 193 */         if (item != null) {
/* 194 */           sb.append(item);
        }
      }

/* 198 */       return sb.toString();
    }

/* 201 */     return String.valueOf(value);
  }

  protected void tagWithText(String name, String text, Object... nameValues) {
/* 205 */     if (text == null) {
      return;
    }

/* 209 */     indent();

/* 211 */     this.m_sb.append('<').append(name);

/* 213 */     int len = nameValues.length;

/* 215 */     for (int i = 0; i + 1 < len; i += 2) {
/* 216 */       Object attrName = nameValues[i];
/* 217 */       Object attrValue = nameValues[i + 1];

/* 219 */       if (attrValue != null) {
/* 220 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 224 */     this.m_sb.append(">");
/* 225 */     this.m_sb.append(escape(text, true));
/* 226 */     this.m_sb.append("</").append(name).append(">\r\n");
  }

  protected void element(String name, String text, String defaultValue, boolean escape) {
/* 230 */     if (text == null || text.equals(defaultValue)) {
      return;
    }

/* 234 */     indent();

/* 236 */     this.m_sb.append('<').append(name).append(">");

/* 238 */     if (escape) {
/* 239 */       this.m_sb.append(escape(text, true));
    } else {
/* 241 */       this.m_sb.append("<![CDATA[").append(text).append("]]>");
    }

/* 244 */     this.m_sb.append("</").append(name).append(">\r\n");
  }

  protected String toString(Date date, String format) {
/* 248 */     if (date != null) {
/* 249 */       return (new SimpleDateFormat(format)).format(date);
    }
/* 251 */     return null;
  }



  public void visitDomain(Domain domain) {
/* 257 */     startTag("domain", null, new Object[] { "id", domain.getId() });

/* 259 */     tagWithText("totalCount", String.valueOf(domain.getTotalCount()), new Object[0]);

/* 261 */     tagWithText("failureCount", String.valueOf(domain.getFailureCount()), new Object[0]);

/* 263 */     tagWithText("failurePercent", String.valueOf(domain.getFailurePercent()), new Object[0]);

/* 265 */     tagWithText("sum", String.valueOf(domain.getSum()), new Object[0]);

/* 267 */     tagWithText("avg", String.valueOf(domain.getAvg()), new Object[0]);

/* 269 */     tagWithText("qps", String.valueOf(domain.getQps()), new Object[0]);

/* 271 */     endTag("domain");
  }


  public void visitServiceReport(ServiceReport serviceReport) {
/* 276 */     startTag("service-report", null, new Object[] { "startTime", toString(serviceReport.getStartTime(), "yyyy-MM-dd HH:mm:ss"), "domain", serviceReport.getDomain(), "endTime", toString(serviceReport.getEndTime(), "yyyy-MM-dd HH:mm:ss") });

/* 278 */     if (!serviceReport.getDomains().isEmpty()) {
/* 279 */       for (Domain domain : serviceReport.getDomains().values()) {
/* 280 */         domain.accept(this.m_visitor);
      }
    }

/* 284 */     endTag("service-report");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/service/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */